import { HTTP } from '../../../utils/http.js'
import api from '../../../utils/api.js'
import { config } from '../../../utils/config'
import utils from "../../../utils/util"
let http = new HTTP()
var app = getApp();
let timer;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: config.imgUrl,
    payBtn : true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let order_no = options.order;
    // let order_no = 'MC20200628628321302690860789';
    this.setData({
      order_no
    })
  
  },
  showView(e){
    let index = e.currentTarget.dataset.index;
    let cookbooks =  this.data.cookbooks
    cookbooks.forEach((item, key) => {
      if(key == index){
        item.showView = !item.showView
      }else{
        item.showView = false
      }
    })
    this.setData({
      cookbooks
    })
  },
  // 获取订单详情
  getOrderDetail(){
    let that = this;
    wx.showLoading({title: '加载中'})
    let order_no = this.data.order_no;
    http.request({
      url: api.orderDetails,
      method: 'GET',
      data: {
          order_no : order_no
      },
      success: (res) => { 
        wx.hideLoading();
        let result = res.data
        if(res.code == 200){
          that.setData(result)

          let cookbooks = this.data.cookbooks
          cookbooks.forEach(item => {
            item.showView = false
          })
          this.setData({
            cookbooks
          })
          if(result.status == 1){
            timer = setInterval (function () {
              that.showtime()
            }, 1000); 
          }
         

        }
          
      },
      fail: (res) => {

      }
  })
  },
 
  /*申请退款*/
  refund(){

    let that = this;
    wx.showModal({
      title : '提示',
      content:'确定申请退款？',
      confirmColor:'#EA5D56',
      success(res){
        if(res.confirm){
          
          http.request({
            url: api.refundOrder,
            method: "POST",
            data: {
                order_no: that.data.order_no,
                refund_cause:'商品质量有问题'
            },
            success: (res) => {
                console.log(res);
                if (res.code == 200) {
                    console.log('申请退款成功');
                    that.getOrderDetail();
                    clearInterval(timer);

                      // 给上一页传递数据
                    const pages = getCurrentPages()
                    const prevPage = pages[pages.length-2]
                    prevPage.setData({
                      refundOrderBack : that.data.order_no
                    })

                }
            },
            fail: (res) => {
        
            }
        })

        }else if(res.cancel){
          return;
        }
      }
    })

    
  
  },
 
  // 电话
  service(e){
    let phone = e.currentTarget.dataset.phone;
    console.log(phone);
    wx.makePhoneCall({
      phoneNumber : phone.toString()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  goAddComment(){
    app.globalData.shops = this.data.shops
    app.globalData.snap_items = this.data.snap_items_text;
    app.globalData.order_no = this.data.order_no;
    wx.navigateTo({
      url: '/pages/addComment/addComment'
    })
  },
  CommentList(e){
    let shopid = e.currentTarget.dataset.shopid;
    wx.navigateTo({
      url : '/pages/comments/comments?id='+this.data.shops.id
    })
  },
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})